/*  input.h
    This file hanldes the button presses of the game boys.
    Important Functions include: buttonPoll(), isButtonDown() and isButtonUp().
*/
#ifndef INPUT_H
#define INPUT_H

// Buttom Memory Location Define
#define BUTTONS (volatile unsigned short*)0x04000130

// Button Defines
#define BUTTON_A 1
#define BUTTON_B 2
#define BUTTON_SELECT 4
#define BUTTON_START 8
#define BUTTON_RIGHT 16
#define BUTTON_LEFT 32
#define BUTTON_UP 64
#define BUTTON_DOWN 128
#define BUTTON_R 256
#define BUTTON_L 512

// Current State and Previous State
unsigned short curr_state, prev_state;

// Updates the Button state
void buttonPoll()
{
    prev_state = curr_state;
    curr_state = ~(*BUTTONS) & 0x03FF;
}

// Returns true if button is down
int buttonIsDown(int button)
{
    return curr_state & button;
}

int buttonIsUp(int button)
{
    return ~curr_state & button;
}

int buttonWasDown(int button)
{
    return prev_state & button;
}

int buttonWasUp(int button)
{
    return ~prev_state & button;
}

//True if button has changed state
int buttonTransition(int button)
{
    return (curr_state ^ prev_state) & button;
}

//True if button is currently pressed and was pressed
int buttonHeld(int button)
{
    return (curr_state & prev_state) & button;
}

//True if button is currently pressed and was unpressed
int buttonHit(int button)
{
    return (curr_state & ~prev_state) & button;
}

//True if button is currently not pressed but was pressed
int buttonReleased(int button)
{
    return (~curr_state & prev_state) & button;
}


#endif

/* END OF FILE */
